<!DOCTYPE HTML>
<html>

<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="utf-8">
  
  <title>第 2 页 | backend | DeepThought</title>
  <meta name="google-site-verification" content="Ei6mmDdf-cJwox1RvJOeX8Dc720fqWjZQu-Ww2BjBt4">
  
  <meta name="author" content="积慕">
  
  <meta name="description" content="个人博客知识分享,知识需沉淀,更需分享">
  
  
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

  
  <meta property="og:site_name" content="DeepThought">

  
  <meta property="og:image" content>
  

  <link href="https://pic.zhuliang.ltd/favicon.ico" rel="shortcut icon" type="image/x-icon">
  <link rel="alternate" href="/atom.xml" title="DeepThought" type="application/atom+xml">
  <link rel="stylesheet" href="/css/style.css" media="screen" type="text/css">

  <link rel="stylesheet" href="//at.alicdn.com/t/font_1187293_8fec2ou5fyx.css">

  <!-- <link rel="stylesheet" href="//cdn.bootcss.com/highlight.js/9.6.0/styles/github.min.css"> -->
  <!-- <script src="//cdn.bootcss.com/highlight.js/9.6.0/highlight.min.js"></script> -->
  <script src="/js/highlight.min.js"></script>

  <link rel="stylesheet" href="/css/markdown.css">

  <script src="/js/av-min.3.0.4.js"></script>
  <script src="/js/Valine.min.1.3.6.js"></script>

</head></html>

<body>
  <header id="header" class="inner"><div class="alignleft">
  <h1><a href="/">DeepThought</a></h1>
  <h2><a href="/">知识需沉淀，更需分享</a></h2>
</div>
<nav id="main-nav" class="alignright">
  <ul>
    
      <li><a href="/">首页</a></li>
    
      <li><a href="/about">关于我</a></li>
    
      <li><a href="/atom.xml">订阅RSS</a></li>
    
  </ul>
  <div class="clearfix"></div>
</nav>
<div class="clearfix"></div>
        </header>
  <div id="content" class="inner">
    <div id="main-col" class="alignleft"><div id="wrapper">
<h2  style="background:#fff; border-bottom: 1px solid #ddd; padding-bottom:17.5px; padding-top:17.5px; padding-left:5px;margin-bottom: 0px;color:#999;font-weight:normal;text-shadow:0 0 1px #fff;">
  <i class="iconfont  icon-category" style="margin-right:5px;"></i>
   分类：backend</h2>


  
    <article class="post">
  
  <div class="post-content">
    <header>
      
        <div class="icon"></div>
      

 
      
  
    <h1 class="title"><a href="/2020/01/backend/Elasticsearch-Nest-Indices-Paths.html">Elasticsearch.Nest 教程系列 9-7 转换：Indices Paths | 索引路径</a></h1>
  


      
        <time datetime="2020-01-23T03:01:01.000Z">
          create: 2020-01-23 11:01:01 | update: 2020-01-23 12:15:44
            <span style="float: right;">
    <span class="post-count" id="busuanzi_container_page_pv" style="display: inline;">
        
        <span class="post-count">文章总字数:&nbsp;972&nbsp;字</span>
    <span class="blueSeparate">&nbsp;|&nbsp;</span>
    <span class="post-count">阅读约需:&nbsp;4&nbsp;分钟</span>
    <br>
</span>
</span>
        </time>
      
            
    </header>
    <div class="entry">
      
      <blockquote>
<ul>
<li>本系列博文是“伪”官方文档翻译（更加本土化），并非完全将官方文档进行翻译，而是在查阅、测试原始文档并转换为自己真知灼见后的“准”翻译。有不同见解 / 说明不周的地方，还请海涵、不吝拍砖 ：）</li>
<li>官方文档见此：<a href="https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/introduction.html" target="_blank" rel="noopener">https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/introduction.html</a></li>
<li>本系列对应的版本环境：<a href="mailto:ElasticSearch@7.3.1" target="_blank" rel="noopener">ElasticSearch@7.3.1</a>，<a href="mailto:NEST@7.3.1" target="_blank" rel="noopener">NEST@7.3.1</a>，IDE 和开发平台默认为 VS2019，.NET CORE 2.1</li>
</ul>
</blockquote>
<hr>
<p>Elasticsearch 中的某些API在请求的 URI 中回需要使用索引名称，索引名称的集合或特殊的 _all 标记（表示所有索引），以指定在哪个(哪些)索引上进行请求。</p>
<p>在 Nest 中，这些索引的名称通过 Indices 类型来进行指定。</p>
<h1 id="显式指定"><a href="#显式指定" class="headerlink" title="显式指定"></a>显式指定</h1><p>以下几种类型会隐式转换：</p>
<ul>
<li>string。</li>
<li>逗号分隔的 string。</li>
<li>string 数组。</li>
<li>已经在 ConnectionSettings 上设置相关类型的索引名称的 CLR 类。</li>
<li>IndexName 类</li>
<li>IndexName 数组</li>
</ul>
      

      <div>
        
      </div>
    </div>

    <footer>
      
      
      <div class="alignleft">
        <a href="/2020/01/backend/Elasticsearch-Nest-Indices-Paths.html#more" class="more-link">Read More</a>
      </div>
      
      
      
      <div class="clearfix"></div>
    </footer>
  </div>
</article>

<!--  --> <!--启用/关闭 LeanCloud 评论-->
  
    <article class="post">
  
  <div class="post-content">
    <header>
      
        <div class="icon"></div>
      

 
      
  
    <h1 class="title"><a href="/2020/01/backend/Elasticsearch-Nest-Documents-path.html">Elasticsearch.Nest 教程系列 9-6 转换：Document paths | 文档路径</a></h1>
  


      
        <time datetime="2020-01-23T01:43:01.000Z">
          create: 2020-01-23 09:43:01 | update: 2020-01-23 12:15:44
            <span style="float: right;">
    <span class="post-count" id="busuanzi_container_page_pv" style="display: inline;">
        
        <span class="post-count">文章总字数:&nbsp;454&nbsp;字</span>
    <span class="blueSeparate">&nbsp;|&nbsp;</span>
    <span class="post-count">阅读约需:&nbsp;2&nbsp;分钟</span>
    <br>
</span>
</span>
        </time>
      
            
    </header>
    <div class="entry">
      
      <blockquote>
<ul>
<li>本系列博文是“伪”官方文档翻译（更加本土化），并非完全将官方文档进行翻译，而是在查阅、测试原始文档并转换为自己真知灼见后的“准”翻译。有不同见解 / 说明不周的地方，还请海涵、不吝拍砖 ：）</li>
<li>官方文档见此：<a href="https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/introduction.html" target="_blank" rel="noopener">https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/introduction.html</a></li>
<li>本系列对应的版本环境：<a href="mailto:ElasticSearch@7.3.1" target="_blank" rel="noopener">ElasticSearch@7.3.1</a>，<a href="mailto:NEST@7.3.1" target="_blank" rel="noopener">NEST@7.3.1</a>，IDE 和开发平台默认为 VS2019，.NET CORE 2.1</li>
</ul>
</blockquote>
<hr>
<p>Elasticsearch中的许多API描述了文档的路径。在NEST中，除了生成分别带有 Index，Type 和 Id 类型的构造函数外，还有一个构造函数允许你使用 DocumentPath<t> 类型的实例更简洁地描述文档的路径。</t></p>
<p>eg：创建一个基于 Project 类的 Document，Id=1</p>
<p></p><p class="code-caption" data-lang="csharp" data-line_number="backend" data-trim_indent="backend" data-label_position="outer" data-labels_left="Code" data-labels_right=":" data-labels_copy="Copy Code"><span class="code-caption-label"></span></p><p></p>
<pre><code class="csharp">IDocumentPath path = new DocumentPath&lt;Project&gt;(1);
</code></pre>
<p><img src="https://pic.zhuliang.ltd/20200123095730.png-c" alt></p>
<p>你也可以指定 Index 的名字：</p>
<p></p><p class="code-caption" data-lang="csharp" data-line_number="backend" data-trim_indent="backend" data-label_position="outer" data-labels_left="Code" data-labels_right=":" data-labels_copy="Copy Code"><span class="code-caption-label"></span></p><p></p>
<pre><code class="csharp">var path = new DocumentPath&lt;Project&gt;(1).Index(&quot;project1&quot;);
</code></pre>
<p>通过 DocumentPath<t> 静态方法来进行指定：</t></p>
      

      <div>
        
      </div>
    </div>

    <footer>
      
      
      <div class="alignleft">
        <a href="/2020/01/backend/Elasticsearch-Nest-Documents-path.html#more" class="more-link">Read More</a>
      </div>
      
      
      
      <div class="clearfix"></div>
    </footer>
  </div>
</article>

<!--  --> <!--启用/关闭 LeanCloud 评论-->
  
    <article class="post">
  
  <div class="post-content">
    <header>
      
        <div class="icon"></div>
      

 
      
  
    <h1 class="title"><a href="/2020/01/backend/Elasticsearch-Nest-Property-name-inference.html">Elasticsearch.Nest 教程系列 9-5 转换：Property name inference | 属性名推断</a></h1>
  


      
        <time datetime="2020-01-22T10:33:01.000Z">
          create: 2020-01-22 18:33:01 | update: 2020-01-23 12:15:44
            <span style="float: right;">
    <span class="post-count" id="busuanzi_container_page_pv" style="display: inline;">
        
        <span class="post-count">文章总字数:&nbsp;352&nbsp;字</span>
    <span class="blueSeparate">&nbsp;|&nbsp;</span>
    <span class="post-count">阅读约需:&nbsp;1&nbsp;分钟</span>
    <br>
</span>
</span>
        </time>
      
            
    </header>
    <div class="entry">
      
      <blockquote>
<ul>
<li>本系列博文是“伪”官方文档翻译（更加本土化），并非完全将官方文档进行翻译，而是在查阅、测试原始文档并转换为自己真知灼见后的“准”翻译。有不同见解 / 说明不周的地方，还请海涵、不吝拍砖 ：）</li>
<li>官方文档见此：<a href="https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/introduction.html" target="_blank" rel="noopener">https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/introduction.html</a></li>
<li>本系列对应的版本环境：<a href="mailto:ElasticSearch@7.3.1" target="_blank" rel="noopener">ElasticSearch@7.3.1</a>，<a href="mailto:NEST@7.3.1" target="_blank" rel="noopener">NEST@7.3.1</a>，IDE 和开发平台默认为 VS2019，.NET CORE 2.1</li>
</ul>
</blockquote>
<hr>
<h1 id="在-lambda-表达式-“主体”-上追加后缀"><a href="#在-lambda-表达式-“主体”-上追加后缀" class="headerlink" title="在 lambda 表达式 “主体” 上追加后缀"></a>在 lambda 表达式 “主体” 上追加后缀</h1><p>可以将后缀追加到 lambda 表达式的主体上，这在以下情况下很有用：</p>
<ul>
<li>将POCO属性映射为多字段，并且希望对 lambda 表达式使用强类型访问：将后缀追加到生成的字段名称中，以便访问多字段的特定子字段。</li>
</ul>
<p>.Suffix() 扩展方法可用于此目的，并且当序列化以此方式追加后缀的表达式时，序列化的属性名称将解析为最后一个标记：</p>
<p></p><p class="code-caption" data-lang="csharp" data-line_number="backend" data-trim_indent="backend" data-label_position="outer" data-labels_left="Code" data-labels_right=":" data-labels_copy="Copy Code"><span class="code-caption-label"></span></p><p></p>
<pre><code class="csharp">Expression&lt;Func&lt;Project, object&gt;&gt; expression = p =&gt; p.Name.Suffix(&quot;raw&quot;);
Expect(&quot;raw&quot;).WhenSerializing&lt;PropertyName&gt;(expression);
</code></pre>
<h1 id="在-lambda-表达式上追加后缀"><a href="#在-lambda-表达式上追加后缀" class="headerlink" title="在 lambda 表达式上追加后缀"></a>在 lambda 表达式上追加后缀</h1><p>可以使用.ApplySuffix() 扩展方法将后缀直接应用于 lambda 表达式：</p>
<p></p><p class="code-caption" data-lang="csharp" data-line_number="backend" data-trim_indent="backend" data-label_position="outer" data-labels_left="Code" data-labels_right=":" data-labels_copy="Copy Code"><span class="code-caption-label"></span></p><p></p>
<pre><code class="csharp">Expression&lt;Func&lt;Project, object&gt;&gt; expression = p =&gt; p.Name;
expression = expression.AppendSuffix(&quot;raw&quot;);
Expect(&quot;raw&quot;).WhenSerializing&lt;PropertyName&gt;(expression);
</code></pre>

      

      <div>
        
      </div>
    </div>

    <footer>
      
      
      
      
      <div class="clearfix"></div>
    </footer>
  </div>
</article>

<!--  --> <!--启用/关闭 LeanCloud 评论-->
  
    <article class="post">
  
  <div class="post-content">
    <header>
      
        <div class="icon"></div>
      

 
      
  
    <h1 class="title"><a href="/2020/01/backend/Elasticsearch-Nest-field-inference.html">Elasticsearch.Nest 教程系列 9-4 转换：Field inference | 字段推断</a></h1>
  


      
        <time datetime="2020-01-22T08:44:01.000Z">
          create: 2020-01-22 16:44:01 | update: 2020-01-23 12:15:44
            <span style="float: right;">
    <span class="post-count" id="busuanzi_container_page_pv" style="display: inline;">
        
        <span class="post-count">文章总字数:&nbsp;2.8k&nbsp;字</span>
    <span class="blueSeparate">&nbsp;|&nbsp;</span>
    <span class="post-count">阅读约需:&nbsp;14&nbsp;分钟</span>
    <br>
</span>
</span>
        </time>
      
            
    </header>
    <div class="entry">
      
      <blockquote>
<ul>
<li>本系列博文是“伪”官方文档翻译（更加本土化），并非完全将官方文档进行翻译，而是在查阅、测试原始文档并转换为自己真知灼见后的“准”翻译。有不同见解 / 说明不周的地方，还请海涵、不吝拍砖 ：）</li>
<li>官方文档见此：<a href="https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/introduction.html" target="_blank" rel="noopener">https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/introduction.html</a></li>
<li>本系列对应的版本环境：<a href="mailto:ElasticSearch@7.3.1" target="_blank" rel="noopener">ElasticSearch@7.3.1</a>，<a href="mailto:NEST@7.3.1" target="_blank" rel="noopener">NEST@7.3.1</a>，IDE 和开发平台默认为 VS2019，.NET CORE 2.1</li>
</ul>
</blockquote>
<hr>
<p>不少 Elasticsearch API 接口在使用的时候会希望知道字段在原始文档中的路径（以字符串的形式），NEST 提供了 Field 类来允许你获得这些字段路径字符串。</p>
<h1 id="构造函数"><a href="#构造函数" class="headerlink" title="构造函数"></a>构造函数</h1><p>通过使用 Field 的构造函数：</p>
<p></p><p class="code-caption" data-lang="csharp" data-line_number="backend" data-trim_indent="backend" data-label_position="outer" data-labels_left="Code" data-labels_right=":" data-labels_copy="Copy Code"><span class="code-caption-label"></span></p><p></p>
<pre><code class="csharp">var fieldString = new Field(&quot;name&quot;);

//使用重载方法
var fieldProperty = new Field(typeof(Project).GetProperty(nameof(Project.Name)));

//使用 lambda 表达式的方式
Expression&lt;Func&lt;Project, object&gt;&gt; expression = p =&gt; p.Name;
var fieldExpression = new Field(expression);
</code></pre>
<p>你也可以在实例化的时候设定 boost 值。</p>
<p>eg：当你使用如下构造函数时：</p>
      

      <div>
        
      </div>
    </div>

    <footer>
      
      
      <div class="alignleft">
        <a href="/2020/01/backend/Elasticsearch-Nest-field-inference.html#more" class="more-link">Read More</a>
      </div>
      
      
      
      <div class="clearfix"></div>
    </footer>
  </div>
</article>

<!--  --> <!--启用/关闭 LeanCloud 评论-->
  
    <article class="post">
  
  <div class="post-content">
    <header>
      
        <div class="icon"></div>
      

 
      
  
    <h1 class="title"><a href="/2020/01/backend/Elasticsearch-Nest-id-inference.html">Elasticsearch.Nest 教程系列 9-3 转换：Ids inference | Id 文档标识推断</a></h1>
  


      
        <time datetime="2020-01-22T08:06:01.000Z">
          create: 2020-01-22 16:06:01 | update: 2020-01-23 12:15:44
            <span style="float: right;">
    <span class="post-count" id="busuanzi_container_page_pv" style="display: inline;">
        
        <span class="post-count">文章总字数:&nbsp;698&nbsp;字</span>
    <span class="blueSeparate">&nbsp;|&nbsp;</span>
    <span class="post-count">阅读约需:&nbsp;3&nbsp;分钟</span>
    <br>
</span>
</span>
        </time>
      
            
    </header>
    <div class="entry">
      
      <blockquote>
<ul>
<li>本系列博文是“伪”官方文档翻译（更加本土化），并非完全将官方文档进行翻译，而是在查阅、测试原始文档并转换为自己真知灼见后的“准”翻译。有不同见解 / 说明不周的地方，还请海涵、不吝拍砖 ：）</li>
<li>官方文档见此：<a href="https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/introduction.html" target="_blank" rel="noopener">https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/introduction.html</a></li>
<li>本系列对应的版本环境：<a href="mailto:ElasticSearch@7.3.1" target="_blank" rel="noopener">ElasticSearch@7.3.1</a>，<a href="mailto:NEST@7.3.1" target="_blank" rel="noopener">NEST@7.3.1</a>，IDE 和开发平台默认为 VS2019，.NET CORE 2.1</li>
</ul>
</blockquote>
<hr>
<p>不少 Elasticsearch API 都需要传递一个 Id 类型的对象。这是一种特殊类型，以下几种类型会自动隐式转换为 Id 类型：</p>
<ul>
<li>Int32</li>
<li>Int64</li>
<li>String</li>
<li>Guid</li>
</ul>
<p>方法签名中，入参如果需要一个 Id 类型，那么传入以上 4 种类型的时候会自动隐式转换为 Id 类型。</p>
<p></p><p class="code-caption" data-lang="csharp" data-line_number="backend" data-trim_indent="backend" data-label_position="outer" data-labels_left="Code" data-labels_right=":" data-labels_copy="Copy Code"><span class="code-caption-label"></span></p><p></p>
<pre><code class="csharp">Id idFromInt = 1;
Id idFromLong = 2L;
Id idFromString = &quot;hello-world&quot;;
Id idFromGuid = new Guid(&quot;D70BD3CF-4E38-46F3-91CA-FCBEF29B148E&quot;);
</code></pre>
<h1 id="从类型中推断-Id"><a href="#从类型中推断-Id" class="headerlink" title="从类型中推断 Id"></a>从类型中推断 Id</h1><p>Nest 并无法直接从一个类型对象中推断出 Id，但提供了 Id.From 方法：</p>
<p>假设你有如下类：</p>
      

      <div>
        
      </div>
    </div>

    <footer>
      
      
      <div class="alignleft">
        <a href="/2020/01/backend/Elasticsearch-Nest-id-inference.html#more" class="more-link">Read More</a>
      </div>
      
      
      
      <div class="clearfix"></div>
    </footer>
  </div>
</article>

<!--  --> <!--启用/关闭 LeanCloud 评论-->
  
    <article class="post">
  
  <div class="post-content">
    <header>
      
        <div class="icon"></div>
      

 
      
  
    <h1 class="title"><a href="/2020/01/backend/Elasticsearch-Nest-Relation-names-inference.html">Elasticsearch.Nest 教程系列 9-2 转换：Relation names inference | 关系名推断</a></h1>
  


      
        <time datetime="2020-01-22T07:43:01.000Z">
          create: 2020-01-22 15:43:01 | update: 2020-01-23 12:15:44
            <span style="float: right;">
    <span class="post-count" id="busuanzi_container_page_pv" style="display: inline;">
        
        <span class="post-count">文章总字数:&nbsp;345&nbsp;字</span>
    <span class="blueSeparate">&nbsp;|&nbsp;</span>
    <span class="post-count">阅读约需:&nbsp;1&nbsp;分钟</span>
    <br>
</span>
</span>
        </time>
      
            
    </header>
    <div class="entry">
      
      <blockquote>
<ul>
<li>本系列博文是“伪”官方文档翻译（更加本土化），并非完全将官方文档进行翻译，而是在查阅、测试原始文档并转换为自己真知灼见后的“准”翻译。有不同见解 / 说明不周的地方，还请海涵、不吝拍砖 ：）</li>
<li>官方文档见此：<a href="https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/introduction.html" target="_blank" rel="noopener">https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/introduction.html</a></li>
<li>本系列对应的版本环境：<a href="mailto:ElasticSearch@7.3.1" target="_blank" rel="noopener">ElasticSearch@7.3.1</a>，<a href="mailto:NEST@7.3.1" target="_blank" rel="noopener">NEST@7.3.1</a>，IDE 和开发平台默认为 VS2019，.NET CORE 2.1</li>
</ul>
</blockquote>
<hr>
<p>当你使用子父级关系的时候，需要将子父级文档索引（添加）到同一个索引中，在 ES 5.x 的时候，你可以通过 <indexname>/<type> 中的 <type> 来进行指定，但从 ES7 开始，已经没有 type 一说（type=_doc），这个时候，你需要用 RelationName 来进行代替。</type></type></indexname></p>
<ul>
<li>关于子父级文档跟 RelationName 的使用，可以见此博文：<a href="https://blog.zhuliang.ltd/2020/01/backend/Elasticsearch-Nest-parent-child-relationships.html">NEST 教程系列 4-5 映射：Parent/Child relationships | 子父级关系映射</a></li>
</ul>
<p></p><p class="code-caption" data-lang="csharp" data-line_number="backend" data-trim_indent="backend" data-label_position="outer" data-labels_left="Code" data-labels_right=":" data-labels_copy="Copy Code"><span class="code-caption-label"></span></p><p></p>
<pre><code class="csharp">var settings = new ConnectionSettings()
    .DefaultMappingFor&lt;CommitActivity&gt;(m =&gt; m
        .IndexName(&quot;projects-and-commits&quot;)
        .RelationName(&quot;commits&quot;)
    )
    .DefaultMappingFor&lt;Project&gt;(m =&gt; m
        .IndexName(&quot;projects-and-commits&quot;)
        .RelationName(&quot;projects&quot;)
    );

var resolver = new RelationNameResolver(settings);
var relation = resolver.Resolve&lt;Project&gt;();
relation.Should().Be(&quot;projects&quot;);

relation = resolver.Resolve&lt;CommitActivity&gt;();
relation.Should().Be(&quot;commits&quot;);
</code></pre>
<ul>
<li>RelationName 通过 DefaultTypeNameInferrer 使用一个 string 来表示转换后的 CLR 类型。</li>
<li>显式的配置 TypeName 不会影响原始 CLR 类型。</li>
</ul>

      

      <div>
        
      </div>
    </div>

    <footer>
      
      
      
      
      <div class="clearfix"></div>
    </footer>
  </div>
</article>

<!--  --> <!--启用/关闭 LeanCloud 评论-->
  
    <article class="post">
  
  <div class="post-content">
    <header>
      
        <div class="icon"></div>
      

 
      
  
    <h1 class="title"><a href="/2020/01/backend/Elasticsearch-Nest-Writing-Aggregations.html">Elasticsearch.Nest 教程系列 8 聚合：Writing Aggregations | 使用聚合</a></h1>
  


      
        <time datetime="2020-01-22T05:18:01.000Z">
          create: 2020-01-22 13:18:01 | update: 2020-01-23 12:15:44
            <span style="float: right;">
    <span class="post-count" id="busuanzi_container_page_pv" style="display: inline;">
        
        <span class="post-count">文章总字数:&nbsp;1.3k&nbsp;字</span>
    <span class="blueSeparate">&nbsp;|&nbsp;</span>
    <span class="post-count">阅读约需:&nbsp;6&nbsp;分钟</span>
    <br>
</span>
</span>
        </time>
      
            
    </header>
    <div class="entry">
      
      <blockquote>
<ul>
<li>本系列博文是“伪”官方文档翻译（更加本土化），并非完全将官方文档进行翻译，而是在查阅、测试原始文档并转换为自己真知灼见后的“准”翻译。有不同见解 / 说明不周的地方，还请海涵、不吝拍砖 ：）</li>
<li>官方文档见此：<a href="https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/introduction.html" target="_blank" rel="noopener">https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/introduction.html</a></li>
<li>本系列对应的版本环境：<a href="mailto:ElasticSearch@7.3.1" target="_blank" rel="noopener">ElasticSearch@7.3.1</a>，<a href="mailto:NEST@7.3.1" target="_blank" rel="noopener">NEST@7.3.1</a>，IDE 和开发平台默认为 VS2019，.NET CORE 2.1</li>
</ul>
</blockquote>
<hr>
<p>可以简单将 ES 中的聚合和 Sql server 中的“聚合函数（如 SUM，COUNT 等”）相关联。聚合可以嵌套，通过聚合可以找出某个字段的最大值，最小值，平均值，以及对字段进行求和操作等复杂数据的构建。</p>
<p>另外，ES 还提出了 buckets（桶） 这个概念，你可以简单理解为相当于是 Sql server 中的分组（GROUP BY），即在 ES 中的称 GROUP BY 为“分桶”。 </p>
<p>关于 Elasticsearch 中的聚合说明，可以<a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations.html" target="_blank" rel="noopener">见此</a>。</p>
<h1 id="编写聚合"><a href="#编写聚合" class="headerlink" title="编写聚合"></a>编写聚合</h1><p>Nest 提供了 3 种方式来让你使用聚合：</p>
<ul>
<li>通过 lambda 表达式的方式。</li>
<li>通过内建的请求对象 AggregationDictionary。</li>
<li>通过结合二元运算符来简化 AggregationDictionary 的使用。</li>
</ul>
<p>假设有以下 Project 类：</p>
<p></p><p class="code-caption" data-lang="csharp" data-line_number="backend" data-trim_indent="backend" data-label_position="outer" data-labels_left="Code" data-labels_right=":" data-labels_copy="Copy Code"><span class="code-caption-label"></span></p><p></p>
<pre><code class="csharp">public class Project
{
    public string Name { get; set; }
    public int Quantity { get; set; }
}
</code></pre>
<p>三种方式的请求命令见下方：</p>
      

      <div>
        
      </div>
    </div>

    <footer>
      
      
      <div class="alignleft">
        <a href="/2020/01/backend/Elasticsearch-Nest-Writing-Aggregations.html#more" class="more-link">Read More</a>
      </div>
      
      
      
      <div class="clearfix"></div>
    </footer>
  </div>
</article>

<!--  --> <!--启用/关闭 LeanCloud 评论-->
  
    <article class="post">
  
  <div class="post-content">
    <header>
      
        <div class="icon"></div>
      

 
      
  
    <h1 class="title"><a href="/2020/01/backend/Elasticsearch-Nest-Selecting-return-fields.html">Elasticsearch.Nest 教程系列 7-3 搜索：Selecting fields to return | 选择需要返回的字段</a></h1>
  


      
        <time datetime="2020-01-21T10:14:01.000Z">
          create: 2020-01-21 18:14:01 | update: 2020-01-23 12:15:44
            <span style="float: right;">
    <span class="post-count" id="busuanzi_container_page_pv" style="display: inline;">
        
        <span class="post-count">文章总字数:&nbsp;553&nbsp;字</span>
    <span class="blueSeparate">&nbsp;|&nbsp;</span>
    <span class="post-count">阅读约需:&nbsp;2&nbsp;分钟</span>
    <br>
</span>
</span>
        </time>
      
            
    </header>
    <div class="entry">
      
      <blockquote>
<ul>
<li>本系列博文是“伪”官方文档翻译（更加本土化），并非完全将官方文档进行翻译，而是在查阅、测试原始文档并转换为自己真知灼见后的“准”翻译。有不同见解 / 说明不周的地方，还请海涵、不吝拍砖 ：）</li>
<li>官方文档见此：<a href="https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/introduction.html" target="_blank" rel="noopener">https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/introduction.html</a></li>
<li>本系列对应的版本环境：<a href="mailto:ElasticSearch@7.3.1" target="_blank" rel="noopener">ElasticSearch@7.3.1</a>，<a href="mailto:NEST@7.3.1" target="_blank" rel="noopener">NEST@7.3.1</a>，IDE 和开发平台默认为 VS2019，.NET CORE 2.1</li>
</ul>
</blockquote>
<hr>
<p>有的时候你不需要文档中的所有字段进行返回，有 2 种方法可以让你只返回文档中的某些字段。</p>
<h1 id="通过-Stored-Fields"><a href="#通过-Stored-Fields" class="headerlink" title="通过 Stored Fields"></a>通过 Stored Fields</h1><p>索引文档时，默认情况下，Elasticsearch 会将最初发送的 JSON 文档存储在名为 _source 的特殊字段中。对于每个匹配，搜索查询返回的文档都是从 Elasticsearch 返回的 _source 字段中解析的。 </p>
<p><strong>注意：</strong> 禁用 _source 意味着不存储原始 JSON 文档，虽然可以节约磁盘，但会导致某些功能无法使用，如无法被检索，无法使用 Reindex API 和高亮。因此禁用 _source 需要根据你的应用场景谨慎考虑。</p>
<p>在搜索请求是通过 .StoredFields 指定要返回的字段值：</p>
      

      <div>
        
      </div>
    </div>

    <footer>
      
      
      <div class="alignleft">
        <a href="/2020/01/backend/Elasticsearch-Nest-Selecting-return-fields.html#more" class="more-link">Read More</a>
      </div>
      
      
      
      <div class="clearfix"></div>
    </footer>
  </div>
</article>

<!--  --> <!--启用/关闭 LeanCloud 评论-->
  
    <article class="post">
  
  <div class="post-content">
    <header>
      
        <div class="icon"></div>
      

 
      
  
    <h1 class="title"><a href="/2020/01/backend/Elasticsearch-Nest-Writing-Queries.html">Elasticsearch.Nest 教程系列 7-2 搜索：Writing bool queries | 编写布尔查询</a></h1>
  


      
        <time datetime="2020-01-21T07:59:01.000Z">
          create: 2020-01-21 15:59:01 | update: 2020-01-23 12:15:44
            <span style="float: right;">
    <span class="post-count" id="busuanzi_container_page_pv" style="display: inline;">
        
        <span class="post-count">文章总字数:&nbsp;1.9k&nbsp;字</span>
    <span class="blueSeparate">&nbsp;|&nbsp;</span>
    <span class="post-count">阅读约需:&nbsp;8&nbsp;分钟</span>
    <br>
</span>
</span>
        </time>
      
            
    </header>
    <div class="entry">
      
      <blockquote>
<ul>
<li>本系列博文是“伪”官方文档翻译（更加本土化），并非完全将官方文档进行翻译，而是在查阅、测试原始文档并转换为自己真知灼见后的“准”翻译。有不同见解 / 说明不周的地方，还请海涵、不吝拍砖 ：）</li>
<li>官方文档见此：<a href="https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/introduction.html" target="_blank" rel="noopener">https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/introduction.html</a></li>
<li>本系列对应的版本环境：<a href="mailto:ElasticSearch@7.3.1" target="_blank" rel="noopener">ElasticSearch@7.3.1</a>，<a href="mailto:NEST@7.3.1" target="_blank" rel="noopener">NEST@7.3.1</a>，IDE 和开发平台默认为 VS2019，.NET CORE 2.1</li>
</ul>
</blockquote>
<hr>
<p>在使用查询 DSL 的时候，编写布尔查询命令会很冗长，如使用带有 2 个 should 子句的单个布尔查询：</p>
<p></p><p class="code-caption" data-lang="csharp" data-line_number="backend" data-trim_indent="backend" data-label_position="outer" data-labels_left="Code" data-labels_right=":" data-labels_copy="Copy Code"><span class="code-caption-label"></span></p><p></p>
<pre><code class="csharp">var searchResults = this.Client.Search&lt;Project&gt;(s =&gt; s
    .Query(q =&gt; q 
        .Bool(b =&gt; b
            .Should(
                bs =&gt; bs.Term(p =&gt; p.Name, &quot;x&quot;),
                bs =&gt; bs.Term(p =&gt; p.Name, &quot;y&quot;)
            )
        )
    )
);
</code></pre>
<p>可以相像，如果有多个嵌套布尔查询，最终代码久会长成这样：</p>
<p><img src="https://pic.zhuliang.ltd/20200121165100.png-c" alt></p>
<p>为了解决代码冗长的问题，Nest 重写了一元操作符（“!”和“+”）以及二元操作符（“||”和“&amp;&amp;”）</p>
<table>
<thead>
<tr>
<th>运算符</th>
<th>对应的操作</th>
</tr>
</thead>
<tbody>
<tr>
<td>&amp;&amp;</td>
<td>must</td>
</tr>
<tr>
<td>\</td>
<td>\</td>
<td></td>
<td>should</td>
</tr>
<tr>
<td>+</td>
<td>filter</td>
</tr>
<tr>
<td>!</td>
<td>must_not</td>
</tr>
</tbody>
</table>
      

      <div>
        
      </div>
    </div>

    <footer>
      
      
      <div class="alignleft">
        <a href="/2020/01/backend/Elasticsearch-Nest-Writing-Queries.html#more" class="more-link">Read More</a>
      </div>
      
      
      
      <div class="clearfix"></div>
    </footer>
  </div>
</article>

<!--  --> <!--启用/关闭 LeanCloud 评论-->
  
    <article class="post">
  
  <div class="post-content">
    <header>
      
        <div class="icon"></div>
      

 
      
  
    <h1 class="title"><a href="/2020/01/backend/Elasticsearch-Nest-Testing-analyzers.html">Elasticsearch.Nest 教程系列 6-2 分析：Testing analyzers | 测试分词器</a></h1>
  


      
        <time datetime="2020-01-20T08:30:01.000Z">
          create: 2020-01-20 16:30:01 | update: 2020-01-23 12:15:44
            <span style="float: right;">
    <span class="post-count" id="busuanzi_container_page_pv" style="display: inline;">
        
        <span class="post-count">文章总字数:&nbsp;1.4k&nbsp;字</span>
    <span class="blueSeparate">&nbsp;|&nbsp;</span>
    <span class="post-count">阅读约需:&nbsp;7&nbsp;分钟</span>
    <br>
</span>
</span>
        </time>
      
            
    </header>
    <div class="entry">
      
      <blockquote>
<ul>
<li>本系列博文是“伪”官方文档翻译（更加本土化），并非完全将官方文档进行翻译，而是在查阅、测试原始文档并转换为自己真知灼见后的“准”翻译。有不同见解 / 说明不周的地方，还请海涵、不吝拍砖 ：）</li>
<li>官方文档见此：<a href="https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/introduction.html" target="_blank" rel="noopener">https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/introduction.html</a></li>
<li>本系列对应的版本环境：<a href="mailto:ElasticSearch@7.3.1" target="_blank" rel="noopener">ElasticSearch@7.3.1</a>，<a href="mailto:NEST@7.3.1" target="_blank" rel="noopener">NEST@7.3.1</a>，IDE 和开发平台默认为 VS2019，.NET CORE 2.1</li>
</ul>
</blockquote>
<hr>
<p>借助 Analyze API，可以方便测试内置/自定义的分词器。</p>
<h1 id="测试内置分词器"><a href="#测试内置分词器" class="headerlink" title="测试内置分词器"></a>测试内置分词器</h1><p>通过 Analyze API，可以查看内置分析器是如何分析一段文本。</p>
<ul>
<li>基于标准分词器<br><p class="code-caption" data-lang="csharp" data-line_number="backend" data-trim_indent="backend" data-label_position="outer" data-labels_left="Code" data-labels_right=":" data-labels_copy="Copy Code"><span class="code-caption-label"></span></p><pre><code class="csharp">var analyzeResponse = _client.Indices.Analyze(a =&gt; a
  .Analyzer(&quot;standard&quot;) 
  .Text(&quot;F# is THE SUPERIOR language :)&quot;)
);
</code></pre>
</li>
</ul>
<p>实际发送的请求如下：</p>
<p></p><p class="code-caption" data-lang="sh" data-line_number="backend" data-trim_indent="backend" data-label_position="outer" data-labels_left="Code" data-labels_right=":" data-labels_copy="Copy Code"><span class="code-caption-label"></span></p><p></p>
<pre><code class="sh">POST /_analyze
{
    &quot;analyzer&quot;: &quot;standard&quot;,
    &quot;text&quot;: [
        &quot;F# is THE SUPERIOR language :)&quot;
    ]
}
</code></pre>
<p>响应结果如下：</p>
      

      <div>
        
      </div>
    </div>

    <footer>
      
      
      <div class="alignleft">
        <a href="/2020/01/backend/Elasticsearch-Nest-Testing-analyzers.html#more" class="more-link">Read More</a>
      </div>
      
      
      
      <div class="clearfix"></div>
    </footer>
  </div>
</article>

<!--  --> <!--启用/关闭 LeanCloud 评论-->
  

  <nav id="pagination">
  
    <a href="/categories/backend/" class="alignleft prev">上一页</a>
  
  
    <a href="/categories/backend/page/3/" class="alignright next">下一页</a>
  
  <div class="clearfix"></div>
</nav>
</div></div>
    <aside id="sidebar" class="alignright">
  
<div class="widget tag">
  <h3 class="title"><i class="iconfont icon-category"></i>&nbsp;&nbsp;分类</h3>
  <ul class="entry">
  
    <li><a href="/categories/Obsidian/">Obsidian</a><small>1</small></li>
  
    <li><a href="/categories/PowerUser/">PowerUser</a><small>4</small></li>
  
    <li><a href="/categories/backend/">backend</a><small>57</small></li>
  
    <li><a href="/categories/frontend/">frontend</a><small>9</small></li>
  
    <li><a href="/categories/operations/">operations</a><small>5</small></li>
  
    <li><a href="/categories/提前退休/">提前退休</a><small>2</small></li>
  
  </ul>
</div>


  
<div class="widget tag">
  <h3 class="title"><i class="iconfont icon-new3"></i>&nbsp;&nbsp;最新文章</h3>
  <ul class="entry">
    
      <li>
        <a href="/2024/11/PowerUser/install-docker-in-pve-lxc.html">1. 通过 PVE LXC 搭建 docker 环境</a>
      </li>
    
      <li>
        <a href="/2024/11/Obsidian/obsidian-render-pie-chart.html">2. 2 套用于 obsidian 的生成时间占用饼图脚本示例，基于 templater， charts，echarts+dataview</a>
      </li>
    
      <li>
        <a href="/2024/10/PowerUser/fix-ios18-samba-readonly-issue.html">3. 解决 ios 18（包括ipadOS 18） 下通过文件(files app)连接 samba 服务器，目录变成只读权限的问题</a>
      </li>
    
      <li>
        <a href="/2023/07/operations/how-to-use-pm2.html">4. PM2 的安装、配置和使用</a>
      </li>
    
      <li>
        <a href="/2023/07/backend/expose-intranet-netcoreapi-to-internet.html">5. 通过 DDNS+显性URL 把内网部署的.NET CORE WEBAPI 项目暴露到公网中</a>
      </li>
    
  </ul>
</div>


  
<div class="widget tag">
  <h3 class="title"><i class="iconfont icon-tag"></i>&nbsp;&nbsp;标签</h3>
  <ul class="entry">
  
    <li><a href="/tags/net-core/">.net core</a><small>12</small></li>
  
    <li><a href="/tags/Debian/">Debian</a><small>2</small></li>
  
    <li><a href="/tags/Docker/">Docker</a><small>1</small></li>
  
    <li><a href="/tags/Elasticsearch/">Elasticsearch</a><small>35</small></li>
  
    <li><a href="/tags/Elasticsearch-Nest系列/">Elasticsearch.Nest系列</a><small>35</small></li>
  
    <li><a href="/tags/Obsidian/">Obsidian</a><small>1</small></li>
  
    <li><a href="/tags/PVE/">PVE</a><small>2</small></li>
  
    <li><a href="/tags/StackExchange-Redis系列/">StackExchange.Redis系列</a><small>12</small></li>
  
    <li><a href="/tags/centos/">centos</a><small>3</small></li>
  
    <li><a href="/tags/flex/">flex</a><small>1</small></li>
  
    <li><a href="/tags/ios/">ios</a><small>1</small></li>
  
    <li><a href="/tags/javascript/">javascript</a><small>1</small></li>
  
    <li><a href="/tags/linux/">linux</a><small>4</small></li>
  
    <li><a href="/tags/nginx/">nginx</a><small>2</small></li>
  
    <li><a href="/tags/nuget/">nuget</a><small>2</small></li>
  
    <li><a href="/tags/react/">react</a><small>6</small></li>
  
    <li><a href="/tags/styled-components/">styled-components</a><small>1</small></li>
  
    <li><a href="/tags/内网穿透/">内网穿透</a><small>4</small></li>
  
    <li><a href="/tags/培训讲义/">培训讲义</a><small>2</small></li>
  
    <li><a href="/tags/基础知识/">基础知识</a><small>2</small></li>
  
    <li><a href="/tags/性能优化/">性能优化</a><small>2</small></li>
  
    <li><a href="/tags/教程/">教程</a><small>64</small></li>
  
    <li><a href="/tags/理财/">理财</a><small>2</small></li>
  
    <li><a href="/tags/生命周期/">生命周期</a><small>1</small></li>
  
    <li><a href="/tags/经验/">经验</a><small>4</small></li>
  
  </ul>
</div>


  
  <script type="text/javascript" charset="utf-8" src="/js/tagcloud.js"></script>
  <script type="text/javascript" charset="utf-8" src="/js/tagcanvas.js"></script>
  <div class="widget tag">
    <h3 class="title"><i class="iconfont icon-cloud1"></i>&nbsp;&nbsp;标签云</h3>
    <div id="myCanvasContainer" class="widget tagcloud">
      <canvas width="250" height="250" id="resCanvas" style="width:100%">
        <a href="/tags/net-core/" style="font-size: 17.14px;">.net core</a> <a href="/tags/Debian/" style="font-size: 11.43px;">Debian</a> <a href="/tags/Docker/" style="font-size: 10px;">Docker</a> <a href="/tags/Elasticsearch/" style="font-size: 18.57px;">Elasticsearch</a> <a href="/tags/Elasticsearch-Nest系列/" style="font-size: 18.57px;">Elasticsearch.Nest系列</a> <a href="/tags/Obsidian/" style="font-size: 10px;">Obsidian</a> <a href="/tags/PVE/" style="font-size: 11.43px;">PVE</a> <a href="/tags/StackExchange-Redis系列/" style="font-size: 17.14px;">StackExchange.Redis系列</a> <a href="/tags/centos/" style="font-size: 12.86px;">centos</a> <a href="/tags/flex/" style="font-size: 10px;">flex</a> <a href="/tags/ios/" style="font-size: 10px;">ios</a> <a href="/tags/javascript/" style="font-size: 10px;">javascript</a> <a href="/tags/linux/" style="font-size: 14.29px;">linux</a> <a href="/tags/nginx/" style="font-size: 11.43px;">nginx</a> <a href="/tags/nuget/" style="font-size: 11.43px;">nuget</a> <a href="/tags/react/" style="font-size: 15.71px;">react</a> <a href="/tags/styled-components/" style="font-size: 10px;">styled-components</a> <a href="/tags/内网穿透/" style="font-size: 14.29px;">内网穿透</a> <a href="/tags/培训讲义/" style="font-size: 11.43px;">培训讲义</a> <a href="/tags/基础知识/" style="font-size: 11.43px;">基础知识</a> <a href="/tags/性能优化/" style="font-size: 11.43px;">性能优化</a> <a href="/tags/教程/" style="font-size: 20px;">教程</a> <a href="/tags/理财/" style="font-size: 11.43px;">理财</a> <a href="/tags/生命周期/" style="font-size: 10px;">生命周期</a> <a href="/tags/经验/" style="font-size: 14.29px;">经验</a>
      </canvas>
    </div>
  </div>




</aside>
    <div class="clearfix"></div>
  </div>
  <footer id="footer" class="inner"><div class="alignleft">

  
  &copy; 2018 - 2024 &nbsp;<a href='/about'>积慕</a>
  
  &nbsp;&nbsp;<a href='https://www.cnblogs.com/deepthought/'>我的博客园地址</a>
  &nbsp;&nbsp;finalclassic#outlook.com
</div>

<div class="clearfix"></div>
<div>
  本站主题基于：<a href='https://github.com/hexojs/hexo-theme-light' rel="external nofollow">hexo-theme-light</a>
</div>
<div id="busuanzi_container_site_pv">
  本站总访问量&nbsp;<span id="busuanzi_value_site_pv"></span>&nbsp;次
</div>
<div style="width:300px;margin:0 auto; padding:20px 0;">
  <a target="_blank" href="http://www.beian.gov.cn/portal/registerSystemInfo?recordcode=31011702004805" style="display:inline-block;text-decoration:none;height:20px;line-height:20px;">
    <img src="https://pic.zhuliang.ltd/20200102164648.png" style="float:left;"><p style="float:left;height:20px;line-height:20px;margin: 0px 0px 0px 5px; color:#939393;">沪公网安备 31011702004805号</p></a>
</div></footer>
  <script src="/js/jquery-2.0.3.min.js"></script>
<script src="/js/jquery.imagesloaded.min.js"></script>
<script src="/js/gallery.js"></script>




<link rel="stylesheet" href="/fancybox/jquery.fancybox.css" media="screen" type="text/css">
<script src="/fancybox/jquery.fancybox.pack.js"></script>
<script type="text/javascript">
(function($){
  $('.fancybox').fancybox();
})(jQuery);
</script>


<script type="text/javascript">
hljs.initHighlightingOnLoad();
</script>

<!-- <script src="/js/busuanzi.pure.mini.js"></script> -->
<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
<script>
    var _hmt = _hmt || [];
    (function () {
        var hm = document.createElement("script");
        hm.src = "https://hm.baidu.com/hm.js?ea793b9c8d6b31df666a6bc15038e246";
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(hm, s);
    })();
</script>
<script>
    (function () {
        var bp = document.createElement('script');
        var curProtocol = window.location.protocol.split(':')[0];
        if (curProtocol === 'https') {
            bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
        }
        else {
            bp.src = 'http://push.zhanzhang.baidu.com/push.js';
        }
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(bp, s);
    })();
</script>
</body>
</html>
